package com.kjj.org.controller;

import com.kjj.org.service.IShopService;
import com.kjj.org.domain.Shop;
import com.kjj.org.query.ShopQuery;
import com.kjj.basic.util.AjaxResult;
import com.kjj.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            Shop shop = shopService.getById(id);
            return AjaxResult.me().setData(shop);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< Shop> list = shopService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody ShopQuery query)
    {
        try {
            PageList<Shop> pageList = shopService.queryPage(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop)
    {
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("店铺入驻失败！"+e.getMessage());
        }
    }
    /**
     * 驳回和通过
     */
    @PutMapping("/check")
    public AjaxResult check(@RequestBody Shop shop){
        try {
            shopService.check(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/activate/{id}/{UUID}")
    public AjaxResult activate(@PathVariable("id")Long id,@PathVariable("UUID")String UUID)
    {
        try {
            if (shopService.checkUUID(UUID))
            {
                Shop shop = shopService.getById(id);
                shop.setState(3);
                shopService.update(shop);
                return AjaxResult.me();
            }
            return AjaxResult.me().setSuccess(false).setMessage("UUID已失效");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("激活失败！"+e.getMessage());
        }
    }
    //获取用户
    @GetMapping("/sendMessage/{id}")
    public AjaxResult sendMessage(@PathVariable("id")Long id)
    {
        try {
            String UUID = shopService.getUUID();
            shopService.sendMessage(id,UUID);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发送邮件失败！"+e.getMessage());
        }
    }
}
